package metro.torino;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public abstract class BaseActivity extends Activity {

	private int TIMEOUT_SEC = 30; 
	private boolean mFlagGetGPSDone = false;
	private boolean mFlagNetworkDone = false;
	private boolean mFlagGPSEnable = true;
	private boolean mFlagNetworkEnable = true;
	private LocationManager mLocationManager;
	private Location mLocationGPS = null;
	private Location mLocationNetwork = null;
	private int mCounts = 0;
	protected Location mLocation;
	protected final Handler mHandler = new Handler();
	protected Dialog mProgressDialog;

	protected Runnable showTime = new Runnable() {
		public void run() {
			mCounts++;
			if (mCounts > TIMEOUT_SEC) {
				mFlagGetGPSDone = true;
				mFlagNetworkDone = true;
			}
			mLocation = getCurrentLocation();

			if (mLocation == null) {
				// bestLocation == null continue wait......
				mHandler.postDelayed(this, 1000);
			} else {
				parseXml();
			}
		}

	};

	protected void initGPS() {
		showProgressAsync();
		mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		startAllUpdate();
		mHandler.postDelayed(showTime, 1000);

		mFlagGPSEnable = mLocationManager.isProviderEnabled("gps");
		mFlagNetworkEnable = mLocationManager.isProviderEnabled("network");
		if (!mFlagGPSEnable) {
			mFlagGetGPSDone = true;
		} else {
			mFlagGetGPSDone = false;
		}
		if (!mFlagNetworkEnable) {
			mFlagNetworkDone = true;
		} else {
			mFlagNetworkDone = false;
		}
		mLocation = null;
		mCounts = 0;
	}

	// Turn on the GPS NETWORK update
	public void startAllUpdate() {
		mLocationManager.requestLocationUpdates("gps", 0, 0, mLocationListener01);
		mLocationManager.requestLocationUpdates("network", 0, 0, mLocationListener02);
	}

	// Turn of GPS NETWORK update
	public void stopAllUpdate() {
		mLocationManager.removeUpdates(mLocationListener01);
		mLocationManager.removeUpdates(mLocationListener02);
	}

	public final LocationListener mLocationListener01 = new LocationListener() {
		public void onLocationChanged(Location location) {
			OnGPSChange();
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

	public final LocationListener mLocationListener02 = new LocationListener() {
		public void onLocationChanged(Location location) {
			OnNetworkChange();
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

	private void OnGPSChange() {
		// GPS NETWORK GPS
		mFlagGetGPSDone = true;
		mFlagNetworkDone = true;
		stopAllUpdate();
	}

	private void OnNetworkChange() {
		mFlagNetworkDone = true;
		mLocationManager.removeUpdates(mLocationListener02);
	}

	private Location getCurrentLocation() {
		Location retLocation = null;
		if ((mFlagGetGPSDone == true && mFlagNetworkDone == true)) {
			mLocationGPS = mLocationManager.getLastKnownLocation("gps");
			mLocationNetwork = mLocationManager.getLastKnownLocation("network");
			if (mLocationGPS == null && mLocationNetwork == null) {
				retLocation = mLocationManager.getLastKnownLocation("passive");
				if (retLocation == null) {
					retLocation = new Location("passive");
				}
			} else {
				// culocationGPS culocationNetwork
				if (mLocationGPS != null) {
					retLocation = mLocationGPS;
				} else {
					retLocation = mLocationNetwork;
				}
			}
			// LOCATION
			stopAllUpdate();
		}
		return retLocation;
	}

	protected void showProgressAsync() {

		mHandler.post(new Runnable() {

			public void run() {
				if (mProgressDialog == null) {
					mProgressDialog = new Dialog(BaseActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
					mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
					mProgressDialog.setContentView(R.layout.progress_layout);
					mProgressDialog.setCancelable(false);
					mProgressDialog.show();
				}

			}
		});
	}

	protected void showProgressAsyncCancelable(final LocationListener locationListener, final Context context,
			final LocationManager locationManager) {
		mHandler.post(new Runnable() {

			public void run() {
				if (mProgressDialog == null) {
					mProgressDialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar) {

						@Override
						public void onBackPressed() {
							locationManager.removeUpdates(locationListener);
							hideProgressAsync();
							super.onBackPressed();
						}
					};
					mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
					mProgressDialog.setContentView(R.layout.progress_layout);
					mProgressDialog.show();
				}
			}
		});
	}

	public void hideProgressAsync() {
		mHandler.post(new Runnable() {

			public void run() {
				if (mProgressDialog != null) {
					try {
						mProgressDialog.dismiss();
					} catch (Exception e) {

					} finally {
						mProgressDialog = null;
					}
				}
			}
		});

	}

	protected abstract void parseXml();
}
